#include "unity.h"

#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#include <cstdlib>
#include <cstdio>
#include <cstring>

#include <iostream>

long getTime()
{
	timeval tv;
	gettimeofday(&tv, NULL);
	long time = 1000000 * tv.tv_sec + tv.tv_usec;
	return time;
}

void errorLog(const char* str)
{
	std::cout << str << std::endl
			  << "errno: " << errno << std::endl
			  << strerror(errno) << std::endl;
}

void errorExit(const char* str)
{
	errorLog(str);
	exit(EXIT_FAILURE);
}

int setNoBlocking(int sockfd)
{
	int opts = fcntl(sockfd, F_GETFL);
	if (opts < 0)
	{
		return -1;
	}
	opts = opts | O_NONBLOCK;
	if (fcntl(sockfd, F_SETFL, opts) < 0)
	{
		return -1;
	}
	return 0;
}

int setTcpNoDelay(int sockfd)
{
	int opt = 1;
	if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) < 0)
		return -1;
	return 0;
}
